package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@RestController
@Slf4j
@RequestMapping("/admin/report")
@Api(tags = "报表控制器")
public class ReportController {
    @Resource
    private ReportService reportService;


    @ApiOperation("获取营业额")
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> getTurnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("[获取营业额数据] 开始获取");
        TurnoverReportVO turnoverReportVO = reportService.getTurnoverStatistics(begin,end);
        log.info("[获取营业额数据] 操作完毕");
        return Result.success(turnoverReportVO);
    }

    @ApiOperation("获取用户数据")
    @GetMapping("/userStatistics")
    public Result<UserReportVO> getUserStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("[获取用户数据] 开始执行");
        UserReportVO userReportVO = reportService.getUserStatistics(begin,end);
        log.info("[获取用户数据] 获取完毕");
        return Result.success(userReportVO);
    }

    @ApiOperation("获取销量前10的商品")
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> getTop10Sales(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("[获取销量前10的商品] 开始执行");
        SalesTop10ReportVO salesTop10ReportVO = reportService.getTop10Sales(begin,end);
        log.info("[获取销量前10的商品] 获取完毕");
        return Result.success(salesTop10ReportVO);
    }

    @ApiOperation("获取订单完成率")
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> getCompletionRate(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("[获取订单完成率] 开始执行");
        OrderReportVO orderReportVO = reportService.getCompletionRate(begin,end);
        log.info("[获取订单完成率] 获取完毕");
        return Result.success(orderReportVO);
    }

    @GetMapping("/export")
    @ApiOperation("导出运营数据报表")
    public void export(HttpServletResponse response){
        // TODO
        reportService.export(response);
    }
}